package gwt.immunology.client.activity;

import com.google.gwt.activity.shared.AbstractActivity;
import com.google.gwt.core.client.GWT;
import com.google.gwt.event.shared.EventBus;
import com.google.gwt.place.shared.Place;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.AcceptsOneWidget;
import gwt.immunology.client.ClientFactory;
import gwt.immunology.client.i18n.ImmunologyI18N;
import gwt.immunology.client.place.AllPatientsPlace;
import gwt.immunology.client.service.PatientsService;
import gwt.immunology.client.service.PatientsServiceAsync;
import gwt.immunology.client.view.AllPatientsView;
import gwt.immunology.shared.GenericDto;
import gwt.immunology.shared.ResultSetDto;

public class AllPatientsActivity extends AbstractActivity implements AllPatientsView.Presenter {
    private AllPatientsPlace place;
	private AllPatientsView allPatientsView;
    private ClientFactory clientFactory;
    private ImmunologyI18N i18n = GWT.create(ImmunologyI18N.class);
    private final PatientsServiceAsync patientsService = GWT.create(PatientsService.class);

    public AllPatientsActivity(AllPatientsPlace place, ClientFactory clientFactory) {
        this.clientFactory = clientFactory;
        this.place = place;
    }

    @Override
    public void start(AcceptsOneWidget panel, EventBus eventBus) {
        allPatientsView = this.clientFactory.getAllPatientsView();
        allPatientsView.setPresenter(this);
        panel.setWidget(allPatientsView.asWidget());
        Window.setTitle(i18n.patientsAll());
        
        ResultSetDto searchResultList = this.place.getSearchResultList();
		if (searchResultList == null) {
        	getAllPatients();
        } else {
        	allPatientsView.setPatients(searchResultList, this.place.getPatientsCount());
        }
    }

    @Override
    public void goTo(Place place) {
        this.clientFactory.getPlaceController().goTo(place);
    }

    @Override
    public void getAllPatients() {
        patientsService.getAllPatients(new AsyncCallback<ResultSetDto>() {

            @Override
            public void onSuccess(ResultSetDto result) {
                allPatientsView.setPatients(result, null);
            }

            @Override
            public void onFailure(Throwable caught) {
                Window.alert("Failure to load patients");
            }
        });
    }

    @Override
    public void deletePatient(Long patientId) {
        patientsService.deletePatient(patientId, new AsyncCallback<Void>() {

            @Override
            public void onFailure(Throwable caught) {
                Window.alert("Failure to delete this patient");
            }

            @Override
            public void onSuccess(Void result) {
                getAllPatients();
                allPatientsView.hideViewEditPatientBox();
                Window.alert("Patient successfully deleted");
            }
        });
    }

    @Override
    public void updatePatientInfo(GenericDto patient) {
        patientsService.editPatientInfo(patient, new AsyncCallback<Void>() {

            @Override
            public void onFailure(Throwable caught) {
                Window.alert("Failure to update this patient");
            }

            @Override
            public void onSuccess(Void result) {
                getAllPatients();
                allPatientsView.hideViewEditPatientBox();
                Window.alert("Patient successfully updated");
            }
        });
    }

}
